﻿using PacoToolKit.Format;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Protocol.SouthGridProtocol.Blocks
{
    public class FileTransferStartBlock
    {


        /// <summary>
        /// 文件性质 2 byte
        /// </summary>
        public string fileType
        {
            get;
            set;
        }

        /// <summary>
        /// 文件总段数
        /// </summary>
        public int fileGroupCount { get; set; }

        /// <summary>
        /// 文件大小
        /// </summary>
        public int fileSize { get; set; }

        /// <summary>
        /// 文件名称
        /// </summary>
        public string fileName { get; set; }

        /// <summary>
        /// 构造
        /// </summary>
        /// <param name="fileType">0000</param>
        /// <param name="fileGroupCount">文件总段数</param>
        /// <param name="fileSize">文件大小</param>
        /// <param name="fileName">文件名称</param>
        public FileTransferStartBlock(string fileType, int fileGroupCount, int fileSize, string fileName)
        {
            this.fileType = fileType;
            this.fileGroupCount = fileGroupCount;
            this.fileSize = fileSize;
            this.fileName = fileName;
        }

        /// <summary>
        /// 数据块转换成报文
        /// </summary>
        /// <returns></returns>
        public string toString()
        {
            string blocks = Formater.ReverseHexString(fileType) +
                Formater.ReverseHexString(Formater.DECtoHEX(fileGroupCount.ToString(), 4)) +
                Formater.ReverseHexString(Formater.DECtoHEX(fileSize.ToString(), 8)) +
                Formater.AdjustLengthBottom(Formater.StringToASCLLHexStr(fileName), 64);//ascii no reverse
            return Formater.BytesToHexStr(Formater.crc16(Formater.HexStringToBytesKeep(blocks)));
            //return blocks + "00";//00 is crc16
        }
    }
}
